﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sunboxsoft.Common;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors;
using System.Text.RegularExpressions;
using WebRef = Sunboxsoft.ASMS.Client.Domain.WebRef;
using Service = Sunboxsoft.ASMS.Client.Domain.Service;
using LocalService = Sunboxsoft.ASMS.Client.Domain.LocalService;
using DevExpress.XtraEditors.Repository;
using Sunboxsoft.WinForm;
namespace Sunboxsoft.ASMS.Client.UI
{
    public static class LookUpEditUtil
    {
        /// <summary>
        /// 绑定主数据
        /// </summary>
        /// <param name="lup"></param>
        /// <param name="mdCode"></param>
        /// <param name="parentCode"></param>
        /// <param name="tag"></param>
        [Obsolete("不要使用这个了")]
        public static void BindMasterData(LookUpEdit lup, string mdCode, string parentCode, int? tag)
        {
            lup.Properties.Columns.Clear();
            LookUpColumnInfo colCode = new LookUpColumnInfo();
            colCode.Caption = "编码";
            colCode.FieldName = "Code";

            LookUpColumnInfo colName = new LookUpColumnInfo();
            colName.Caption = "名称";
            colName.FieldName = "Name";

            lup.Properties.Columns.AddRange(new LookUpColumnInfo[] { colCode, colName });
            ListResult<WebRef.EMasterDataItem> result = new LocalService.QueryService().QueryMasterDataItemList(mdCode, parentCode, tag);
            if (result.IsSuccess)
            {
                lup.Properties.DataSource = result.Data.Items;
                lup.Properties.ValueMember = "Code";
                lup.Properties.DisplayMember = "Name";
                lup.EditValue = null;
            }
        }

        [Obsolete("不要使用这个了")]
        public static void BindMasterDataWithClear(LookUpEdit lup, string mdCode, string parentCode, int? tag)
        {
            lup.Properties.Buttons.Add(new EditorButton("clear", ButtonPredefines.Close));
            lup.Properties.ButtonClick += new ButtonPressedEventHandler(OnMasterDataLookUpEditButtonClick);

            lup.Properties.Columns.Clear();
            LookUpColumnInfo colCode = new LookUpColumnInfo();
            colCode.Caption = "编码";
            colCode.FieldName = "Code";

            LookUpColumnInfo colName = new LookUpColumnInfo();
            colName.Caption = "名称";
            colName.FieldName = "Name";

            lup.Properties.Columns.AddRange(new LookUpColumnInfo[] { colCode, colName });
            ListResult<WebRef.EMasterDataItem> result = new LocalService.QueryService().QueryMasterDataItemList(mdCode, parentCode, tag);
            if (result.IsSuccess)
            {
                lup.Properties.DataSource = result.Data.Items;
                lup.Properties.DisplayMember = "Name";
            }
        }

        static void OnMasterDataLookUpEditButtonClick(object sender, ButtonPressedEventArgs e)
        {
            if (e.Button.Tag != null && string.Equals("clear", e.Button.Tag.ToString()))
            {
                LookUpEdit lookUpEdit = sender as LookUpEdit;
                if (lookUpEdit != null)
                {
                    lookUpEdit.EditValue = null;
                }
            }
        }

        /// <summary>
        /// 绑定常量状态
        /// </summary>
        [Obsolete("不要使用这个了")]
        public static void BindCommonStatus(LookUpEdit lup)
        {
            lup.Properties.DataSource = Domain.Constant.CommonStatus.Enumerate();
            lup.Properties.DisplayMember = "Text";
        }

        /// <summary>
        /// 绑定维修单状态
        /// </summary>
        /// <param name="lup"></param>
        public static void BindMaintenanceBillStatus(LookUpEdit lup)
        {
            lup.Properties.DataSource = Domain.Constant.MaintenanceBillStatus.Enumerate();
            lup.Properties.DisplayMember = "Text";
        }

        /// <summary>
        /// 获取LookUpEdit选择常量的Code
        /// </summary>
        /// <param name="lup"></param>
        /// <returns></returns>
        public static int GetIntCode(LookUpEdit lup)
        {
            Domain.IntCodeTextItem item = lup.GetSelectedDataRow() as Domain.IntCodeTextItem;
            if (item == null)
            {
                return Domain.Constant.BookingBillStatus.All;
            }
            return item.Code;
        }

        [Obsolete("use GetSelectedValue")]
        public static T GetSelectObject<T>(LookUpEdit lookUp) where T : class
        {
            object selectRow = lookUp.GetSelectedDataRow();
            if (selectRow == null)
            {
                return default(T);
            }
            return selectRow as T;
        }

        /// <summary>
        /// 选取选择的值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="lookUpEdit"></param>
        /// <returns></returns>
        public static T GetSelectedValue<T>(this LookUpEdit lookUpEdit) where T : class
        {
            T selectedValue = lookUpEdit.EditValue as T;
            return selectedValue;
        }

        public static int GetSelectedIntValue(this LookUpEdit lookUpEdit, int defaultValue = 0)
        {
            if (lookUpEdit.EditValue == null)
            {
                return defaultValue;
            }
            int value = 0;
            if (int.TryParse(lookUpEdit.EditValue.ToString(), out value))
            {
                return value;
            }
            return defaultValue;
        }

        /// <summary>
        /// 绑定销售经理
        /// </summary>
        /// <param name="lup"></param>
        [Obsolete("不要使用这个了")]
        public static void BindSalesManager(LookUpEdit lup)
        {
            lup.Properties.Buttons.Add(new EditorButton("clear", ButtonPredefines.Close));
            lup.Properties.ButtonClick += new ButtonPressedEventHandler(OnMasterDataLookUpEditButtonClick);

            lup.Properties.Columns.Clear();
            LookUpColumnInfo colCode = new LookUpColumnInfo();
            colCode.Caption = "编码";
            colCode.FieldName = "StaffId";
            colCode.Visible = false;

            LookUpColumnInfo colName = new LookUpColumnInfo();
            colName.Caption = "名称";
            colName.FieldName = "StaffName";

            lup.Properties.Columns.AddRange(new LookUpColumnInfo[] { colCode, colName });
            ListResult<WebRef.VStaffPosition> result = new Service.QueryService().QueryStaffPositionList(
                null,
                ModuleContext.Current.BusinessId.Value,
                ModuleContext.Current.StationId,
                Domain.Constant.StaffPosts.SalesManager);
            if (result.IsSuccess)
            {
                lup.Properties.DataSource = result.Data.Items;
                lup.Properties.DisplayMember = "StaffName";
                lup.Properties.ValueMember = "StaffId";
            }
        }


        /// <summary>
        /// 绑定常量数据项
        /// </summary>
        /// <param name="lookUpEdit"></param>
        /// <param name="promise"></param>
        /// <param name="items"></param>
        public static void BindIntCodeTextItem(this RepositoryItemLookUpEdit lookUpEdit, Domain.IntCodeTextItem[] items)
        {
            lookUpEdit.NullText = string.Empty;
            if (lookUpEdit.Columns.Count == 0)
            {
                LookUpColumnInfo colName = new LookUpColumnInfo();
                colName.Caption = "名称";
                colName.FieldName = "Text";

                lookUpEdit.Columns.AddRange(new LookUpColumnInfo[] { colName });
            }

            lookUpEdit.DataSource = items;
            lookUpEdit.DisplayMember = "Text";
        }

        /// <summary>
        /// 将IntCodeTextItem数组绑定到LookUpEdit上
        /// </summary>
        /// <param name="lookUpEdit"></param>
        /// <param name="items"></param>
        /// <param name="defaultItem"></param>
        /// <param name="canClear"></param>
        public static void BindIntCodeTextItem(this LookUpEdit lookUpEdit, Domain.IntCodeTextItem[] items,
            Domain.IntCodeTextItem defaultItem = null,
            bool canClear = false)
        {
            lookUpEdit.Properties.NullText = string.Empty;
            if (lookUpEdit.Properties.Columns.Count == 0)
            {
                lookUpEdit.Properties.PopupWidth = 120;
                lookUpEdit.Properties.ShowFooter = false;
                if (canClear)
                {
                    lookUpEdit.Properties.Buttons.Add(new EditorButton("clear", ButtonPredefines.Close));
                    lookUpEdit.Properties.ButtonClick += new ButtonPressedEventHandler(OnLookUpEditButtonClick);
                }

                LookUpColumnInfo colName = new LookUpColumnInfo();
                colName.Caption = "名称";
                colName.FieldName = "Text";
                lookUpEdit.Properties.Columns.AddRange(new LookUpColumnInfo[] { colName });
            }

            lookUpEdit.Properties.DataSource = items;
            lookUpEdit.Properties.DisplayMember = "Text";
            if (defaultItem != null)
            {
                lookUpEdit.EditValue = defaultItem;
            }
        }


        /// <summary>
        /// 绑定维修单的查询状态
        /// </summary>
        /// <param name="lookUpEdit"></param>
        /// <param name="canClear"></param>
        public static void BindMaintenanceBillQueryStatus(this LookUpEdit lookUpEdit,
            bool canClear = false)
        {
            lookUpEdit.Properties.NullText = string.Empty;
            if (lookUpEdit.Properties.Columns.Count == 0)
            {
                if (canClear)
                {
                    lookUpEdit.Properties.Buttons.Clear();
                    lookUpEdit.Properties.Buttons.Add(new EditorButton("clear", ButtonPredefines.Close));
                    lookUpEdit.Properties.ButtonClick += new ButtonPressedEventHandler(OnLookUpEditButtonClick);
                }
                LookUpColumnInfo colName = new LookUpColumnInfo();
                colName.Caption = "名称";
                colName.FieldName = "Name";
                lookUpEdit.Properties.Columns.AddRange(new LookUpColumnInfo[] { colName });
            }

            lookUpEdit.Properties.DataSource = Domain.Constant.MaintenanceBillQueryStatus.Enumerate(); ;
            lookUpEdit.Properties.DisplayMember = "Name";
            lookUpEdit.EditValue = Domain.Constant.MaintenanceBillQueryStatus.All;
        }

        static void OnLookUpEditButtonClick(object sender, ButtonPressedEventArgs e)
        {
            if (e.Button.Tag != null && string.Equals("clear", e.Button.Tag.ToString()))
            {
                LookUpEdit lookUpEdit = sender as LookUpEdit;
                if (lookUpEdit != null)
                {
                    lookUpEdit.EditValue = null;
                }
            }
        }
    }
}
